<?php
/**
 * 商品分类
 * Created by PhpStorm.
 * User: sunku
 * Date: 2017/12/19
 * Time: 11:49
 */

namespace Resource\Service;
use Admin\Transformer\ItemCateTransformer;
use Think\Model;

class ItemCateService extends Model
{
    protected $trueTableName = 'wz_item_category';
    /**
     * 广告列表
     * */
    public function searchItemCateList($params){
        $page = 1;
        $page_size = 15;
        isset($params['page']) && $page = $params['page'];
        isset($params['page_size']) && $page_size = $params['page_size'];

        $data = [];
        $data['status'] = ['in',[-1,0,1]];
        isset($params['depth']) && $data['depth'] = $params['depth'];
        isset($params['name']) && $data['name'] = ['like',"%{$params['name']}%"];
        isset($params['status']) && $data['status'] = $params['status'];

        //查询总数
        $total = D('Resource/ItemCate')->getCateCount($data);

        //计算总页数
        $total_page = ceil($total / $page_size);
        if($page < 1) {
            $page = 1;
        }
        if($page > $total_page) {
            $page = $total_page;
        }
        //查询列表
        $list = D('Resource/ItemCate')->getCateList($data, $page, $page_size);
        if(is_array($list) && count($list)>0){
            foreach ($list as $k=>$cate){
                if($cate['pid']>0){
                    $p_cate = D('Resource/ItemCate')->getCateInfo(['id'=>$cate['pid']]);
                    $list[$k]['parent_name'] = $p_cate['name'];
                }
            }
        }
        $tr = new ItemCateTransformer();
        $list = $tr->TransformerCollection($list);

        $result = [];
        $result['page'] = $page;
        $result['page_size'] = $page_size;
        $result['total'] = $total;
        $result['total_page'] = $total_page;
        $result['list'] = $list;
        return $result;
    }

    /**
     * @param $params
     * @return mixed
     */
    public function addCate($params) {
        $params['create_time'] = time();
        $params['update_time'] = time();
        return D('Resource/ItemCate')->addCate($params);
    }

    /**
     * @param $params
     * @return mixed
     */
    public function updateCate($params) {
        $params['update_time'] = time();
        return D('Resource/ItemCate')->saveCate($params);
    }

    /**
     * @param $params
     * @param string $fields
     * @return mixed
     */
    public function getCateInfo($params,$fields=''){
        $dataInfo = D('Resource/ItemCate')->getCateInfo($params, $fields);
        return $dataInfo;
    }
    /**
     * @param $params
     * @return mixed
     */
    public function getAllCateList($params){
        $dataInfo = D('Resource/ItemCate')->getAllCateList($params);
        return $dataInfo;
    }

    /**
     * @param $params
     * @return mixed
     */
    public function getCateName($params){
        $dataInfo = D('Resource/ItemCate')->getCateName($params);
        return $dataInfo;
    }
}